<!-- eslint-disable no-unused-vars -->


<template>
  <div class="background">
    <div class="head"></div>
    <div
      class="container"
      id="container"
      @mousemove="handleMousemove($event)"
      @mousedown="handleMousedown($event)"
      @mouseup="handleMouseup($event)"
    >
      <div class="code-container" id="codeContainer">
        <div class="left-container" id="leftContainer">
          <div class="container-topbar">
            <div class="topbar-item" @click="languageChange">
              <font-awesome-icon icon="language" style="color: #5f6477" />
            </div>
            <div class="topbar-item" style="border-right: none">
              <span>元数据</span>
            </div>
          </div>
        </div>
        <div
          class="splitpane-handle-vertical"
          id="splitpaneVertical1"
          style="left: 30%"
        ></div>
        <div class="middle-container" id="middleContainer"></div>
        <div
          class="splitpane-handle-vertical"
          id="splitpaneVertical2"
          style="left: 70%"
        ></div>
        <div class="right-container" id="rightContainer">
          <div class="container-topbar" style="left: 18px">
            <div class="topbar-item">
              <span>资源推荐</span>
            </div>
            <div class="topbar-item">
              <span>逻辑模型</span>
            </div>
          </div>
        </div>
      </div>
      <div class="splitpane-handle-horizontal" id="splitpaneHorizontal1"></div>

      <div
        class="visual-container"
        id="visualContainer"
        :v-model="languageCurent"
      >
        <div class="container-topbar" style="top: 15px">
          <div class="topbar-item" @click="changeContainer('mapContainer')">
            <span>{{ languageCurent.visualContainer.topbar[0] }}</span>
          </div>

          <div
            class="topbar-item"
            style="border-right: none"
            @click="changeContainer('chartContainer')"
          >
            {{ languageCurent.visualContainer.topbar[1] }}
          </div>
          <!-- <el-button type="primary" v-model="languageCurent">{{
            languageCurent.visualContainer.topbar[0]
          }}</el-button> -->
        </div>
        <div
          id="mapContainer"
          class="map-container"
          v-show="topbarShower.visualContainer.map"
        >
          <div class="map-card">
            <font-awesome-icon
              icon="layer-group"
              class="icon"
              @click="showLayerType = !showLayerType"
            />
            <font-awesome-icon
              icon="square-plus"
              class="icon"
              @click="zoom('zoomIn')"
            />
            <font-awesome-icon
              icon="square-minus"
              class="icon"
              @click="zoom('zoomOut')"
            />
          </div>
          <div class="layers" v-show="showLayerType">
            <div class="layer-box" @click="changeLayer('tdtMap')">
              <img src="../assets/img/tdtMap.png" alt="" />
              <p>地图</p>
            </div>
            <div class="layer-box" @click="changeLayer('tdtImg')">
              <img src="../assets/img/tdtImg.png" alt="" />
              <p>影像</p>
            </div>
          </div>
        </div>
        <div
          id="chartContainer"
          class="chart-container"
          v-show="topbarShower.visualContainer.chart"
        ></div>
      </div>
    </div>
  </div>
</template>
<script>
import { Map, View } from "ol";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import OSM from "ol/source/OSM.js";
import { defaults as defaultControls } from "ol/control.js";
export default {
  data() {
    return {
      /**1.分隔条拖拽相关变量 */
      containerWidth: 0,
      containerHeigth: 0,
      // 第一个分割线左边距
      splitpaneVertical1Left: "",
      splitpaneVertical2Left: "",
      // 鼠标行为标记
      mouseActTag: {
        mousedown: false,
        mouseup: false,
      },
      // 拖拽的分隔条
      activeSplitpane: null,
      topbarShower: {
        visualContainer: { map: true, chart: false },
      },

      /**语言切换 */
      languageTag: true,
      languageCurent: { visualContainer: { topbar: [] } },
      language: {
        English: {
          visualContainer: {
            topbar: ["Map", "Chart"],
          },
        },
        Chinese: {
          visualContainer: {
            topbar: ["地图", "图表"],
          },
        },
      },
      /**地图相关变量 */
      map: new Map(),
      showLayerType: false, //切换地图类型
      layers: [],
    };
  },
  methods: {
    resize() {},
    // 鼠标按下事件
    handleMousedown(e) {
      this.refreshContainerSize();
      this.mouseActTag.mousedown = true;
      this.activeSplitpane = e.target;
    },
    // 鼠标移动事件
    handleMousemove(e) {
      // 控制分隔条
      if (this.mouseActTag.mousedown) {
        var percentageValue = (e.x * 100) / this.containerWidth;
        let leftContainer = document.getElementById("leftContainer");
        let middleContainer = document.getElementById("middleContainer");
        let rightContainer = document.getElementById("rightContainer");
        if (this.activeSplitpane.id == "splitpaneVertical1") {
          // 左边的分隔条，记录左边距，第一个分割条的左边距不能大于第二个分隔条
          let splitpaneVertical2 =
            document.getElementById("splitpaneVertical2");
          let splitpane2Value = Number(
            splitpaneVertical2.style.left.split("%")[0]
          ); //此时第2个分隔条的左边距

          if (
            percentageValue >
            splitpane2Value + (-8 * 100) / this.containerWidth
          ) {
            percentageValue =
              splitpane2Value + (-8 * 100) / this.containerWidth;
          }
          // 记录此刻第一个分隔条左边距的大小
          this.activeSplitpane.style.left = percentageValue + "%";
          this.splitpaneVertical1Left = this.activeSplitpane.style.left;

          // 左边和中间的容器大小改变
          leftContainer.style.width = percentageValue + "%";
          middleContainer.style.width = splitpane2Value - percentageValue + "%";
        } else if (this.activeSplitpane.id == "splitpaneVertical2") {
          // 如果右边分隔条左边距>=左边分隔条左边距
          let splitpaneVertical1 =
            document.getElementById("splitpaneVertical1");
          let splitpane1Value = Number(
            splitpaneVertical1.style.left.split("%")[0]
          ); //此时第1个分隔条的左边距
          if (
            percentageValue <
            splitpane1Value + (8 * 100) / this.containerWidth
          ) {
            percentageValue = splitpane1Value + (8 * 100) / this.containerWidth;
          }
          // 记录此刻第二个分割条的左边距
          this.activeSplitpane.style.left = percentageValue + "%";
          this.splitpaneVertical2Left = this.activeSplitpane.style.left;

          // 右边和中间的容器大小改变
          middleContainer.style.width =
            percentageValue -
            Number(this.splitpaneVertical1Left.split("%")[0]) +
            "%";

          rightContainer.style.width = 100 - percentageValue + "%";
        } else if (this.activeSplitpane.id == "splitpaneHorizontal1") {
          //动态改变分隔条位置
          percentageValue = ((e.y - 71) * 100) / this.containerHeigth;
          this.activeSplitpane.style.top = percentageValue + "%";
          // 动态更改上下容器的大小
          let codeContainer = document.getElementById("codeContainer");
          let visualContainer = document.getElementById("visualContainer");
          codeContainer.style.height = percentageValue + "%";
          visualContainer.style.height = 100 - percentageValue + "%";
        }
      }
    },
    // 鼠标松开事件
    handleMouseup() {
      this.mouseActTag.mouseup = true;
      this.mouseActTag.mousedown = false;
    },
    // 更新container的宽和高
    refreshContainerSize() {
      var divElement = document.getElementById("container");
      this.containerWidth = divElement.offsetWidth;
      this.containerHeigth = divElement.offsetHeight;
    },
    // container切换
    changeContainer(type) {
      switch (type) {
        case "mapContainer":
          this.topbarShower.visualContainer.map = true;
          this.topbarShower.visualContainer.chart = false;
          break;
        case "chartContainer":
          this.topbarShower.visualContainer.map = false;
          this.topbarShower.visualContainer.chart = true;
          break;
        default:
          break;
      }
    },
    // 语言切换
    languageChange() {
      this.languageTag = !this.languageTag;
      if (this.languageTag) {
        this.languageCurent = this.language.Chinese;
      } else {
        this.languageCurent = this.language.English;
      }
    },
    /**地图相关方法 */
    // 地图初始化
    initMap() {
      /**图层 */
      /**谷歌地图图层 */
      // eslint-disable-next-line no-unused-vars
      const googleMapTerLayer = new TileLayer({
        source: new XYZ({
          attributions:
            'Tiles © <a href="https://services.arcgisonline.com/ArcGIS/' +
            'rest/services/World_Topo_Map/MapServer">ArcGIS</a>',
          url:
            "https://server.arcgisonline.com/ArcGIS/rest/services/" +
            "World_Topo_Map/MapServer/tile/{z}/{y}/{x}",
        }),
      }); //谷歌地图
      /**天地图图层 */
      const tdtMapVecLayer = new TileLayer({
        source: new XYZ({
          url: "http://t4.tianditu.com/DataServer?T=vec_w&tk=ba83a5dc9758ca23aea593a39d998825&x={x}&y={y}&l={z}",
        }),
      }); //天地图矢量图
      this.layers.push(tdtMapVecLayer);
      // eslint-disable-next-line no-unused-vars
      const tdtMapImgLayer = new TileLayer({
        source: new XYZ({
          url: "http://t4.tianditu.com/DataServer?T=img_w&tk=ba83a5dc9758ca23aea593a39d998825&x={x}&y={y}&l={z}",
        }),
      }); //天地图影像地图
      this.layers.push(tdtMapImgLayer);

      var tdtMapCva = new TileLayer({
        source: new XYZ({
          url: "http://t4.tianditu.com/DataServer?T=cva_w&tk=ba83a5dc9758ca23aea593a39d998825&x={x}&y={y}&l={z}",
        }),
      }); //天地图注记

      // eslint-disable-next-line no-unused-vars
      const OSMLayer = new TileLayer({
        source: new OSM(),
      }); //OSM地图
      const view = new View({
        center: [12914838.35, 4814529.9],
        zoom: 8,
        maxZoom: 18,
        projection: "EPSG:3857",
        constrainResolution: true, // 设置缩放级别为整数
        smoothResolutionConstraint: false, // 关闭无级缩放地图
      });
      this.map = new Map({
        target: "mapContainer",
        logo: false,
        layers: [tdtMapVecLayer, tdtMapCva],
        view: view,
        controls: defaultControls({
          zoom: false,
          attribution: false,
          rotate: false,
        }),
      });
    },
    // 判断map是否包含当前图层
    uniqueLayer(layer) {
      var mapTileLayers = this.map.getLayers().array_;
      for (let index = 0; index < mapTileLayers.length; index++) {
        const mapLayer = mapTileLayers[index];
        if (layer.ol_uid === mapLayer.ol_uid) {
          return true;
        }
      }
      return false;
    },
    // 地图切换
    changeLayer(type) {
      switch (type) {
        case "tdtMap":
          //天地图矢量地图
          if (!this.uniqueLayer(this.layers[0])) {
            this.map.getLayers().removeAt(0);
            this.map.getLayers().insertAt(0, this.layers[0]);
          }
          break;
        case "tdtImg":
          //天地图影像地图
          if (!this.uniqueLayer(this.layers[1])) {
            this.map.getLayers().removeAt(0);
            this.map.getLayers().insertAt(0, this.layers[1]);
          }
          break;
        default:
          break;
      }
    },
    zoom(type) {
      const view = this.map.getView();
      const zoom = view.getZoom();
      switch (type) {
        case "zoomIn":
          view.setZoom(zoom + 1);
          break;
        case "zoomOut":
          view.setZoom(zoom - 1);
          break;
        default:
          break;
      }
    },
  },
  mounted() {
    this.splitpaneVertical1Left = "30%";
    this.splitpaneVertical2Left = "70%";
    this.languageCurent = this.language.Chinese;
    this.initMap(); //地图初始化
  },
  components: {},
};
</script>
<style lang="less" scoped>
.background {
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  position: absolute;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}
.head {
  box-sizing: border-box;
  width: 100%;
  height: 65px;
  background: black;
}

.container {
  box-sizing: border-box;
  width: 100%;
  height: calc(100% - 71px);
  display: flex;
  flex-direction: column;
  margin-top: 6px;
  position: relative;
  border-radius: 3px;
  background: #fff;
  border-color: #f3f3f3;
  border-width: 2px;
  border-style: solid;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
}
.container .code-container {
  width: 100%;
  height: 50%;
  // background: black;
  box-sizing: border-box;
  display: flex;
  position: relative;
}
.container .visual-container {
  width: 100%;
  height: 50%;
  // background: black;
  box-sizing: border-box;
  position: relative;
  padding-top: 8px;
}
.container .left-container {
  width: 30%;
  height: 100%;
  // background: red;
  box-sizing: border-box;
  position: relative;
}
.container .middle-container {
  width: 40%;
  height: 100%;
  // background: green;
  box-sizing: border-box;
  padding-left: 8px;
}
.container .right-container {
  width: 30%;
  height: 100%;
  // background: blue;
  box-sizing: border-box;
  padding-left: 8px;
  position: relative;
}
.container .splitpane-handle-vertical {
  background-color: #f5f5f5;
  background-repeat: no-repeat;
  background-position: center;

  background-image: url(../assets/img/vertical_handle.png),
    -webkit-linear-gradient(left, #f5f5f5, #f1f1f1);
  cursor: col-resize;
  border-left-width: 1px;
  border-right-width: 1px;
  width: 8px;
  position: absolute;
  overflow: hidden;
  z-index: 2;
  left: 1010.81px;
  top: 0px;
  box-sizing: border-box;
  height: 100%;
}

.container .splitpane-handle-horizontal {
  background-color: #f5f5f5;
  background-repeat: no-repeat;
  background-position: center;
  border-color: #dcdcdc;
  border-style: solid;
  border-width: 0;

  background-image: url("../assets/img/handle.png"),
    -webkit-linear-gradient(top, #f5f5f5, #f1f1f1);
  background-position-y: 0;
  cursor: row-resize;
  border-top-width: 1px;
  border-bottom-width: 1px;
  width: 100%;
  height: 8px;
  position: absolute;
  overflow: hidden;
  z-index: 10;
  left: 0px;
  top: 50%;
  box-sizing: border-box;
}
.container-topbar {
  position: absolute;
  z-index: 9;
  box-sizing: border-box;
  top: 5px;
  left: 10px;
  width: fit-content;
  height: 36px;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
  background-color: #fff;
  border-radius: 5px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 5px;

  // background-color: red;
}
.container-topbar .topbar-item {
  float: right;
  height: fit-content;
  padding: 0 12px;
  font-size: 12px;
  border-right: 1px #dbdee2 dashed;
  vertical-align: middle;
  cursor: pointer;
  overflow: visible;
  zoom: 1;
  color: #5f6477;
  font-size: 15px;
}
.container-topbar .topbar-item:hover {
  color: black;
}

.visual-container .map-container {
  width: 100%;
  height: 100%;
}
.visual-container .map-container .map-card {
  position: absolute;
  z-index: 9;
  display: flex;
  flex-direction: column;
  left: 10px;
  bottom: 6px;
  font-size: 18px;
  cursor: pointer;
  justify-content: space-between;
  height: 66px;
  width: 20px;
  padding: 5px 3px;
  background-color: #fff;
  color: rgb(110, 105, 102);
  border-radius: 5px;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
}
.visual-container .map-container .map-card .icon:hover {
  color: rgb(20, 121, 215);
}

.visual-container .map-container .layers {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  position: absolute;
  left: 40px;
  bottom: 42px;
  z-index: 9;
  background: #fff;
  padding: 5px 5px;
  border-radius: 5px;
  box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3),
    0 1px 3px 1px rgba(60, 64, 67, 0.15);
}
.visual-container .map-container .layers .layer-box {
  margin: 0 2px;
  position: relative;
  height: fit-content;
  width: fit-content;
  border: 1px solid rgb(204, 201, 201);

  border-radius: 2px;
}
.visual-container .map-container .layers .layer-box:hover {
  border-color: rgb(20, 121, 215);
}

.visual-container .map-container .layers .layer-box:hover p {
  background: rgb(20, 121, 215);
}

.visual-container .map-container .layers .layer-box img {
  width: 60px;
  height: 50px;
  object-fit: cover;
}

.visual-container .map-container .layers .layer-box p {
  position: absolute;
  bottom: 0;
  left: 0;
  font-size: 12px;
  margin: 0px;
  color: #fff;
  padding: 2px 4px;
  background: rgba(128, 128, 128, 0.664);
}
.visual-container .chart-container {
  width: 100%;
  height: 100%;
}
</style>